# gozerbot/config.py
#
#

""" 
    gozerbot config module. 

    :var config: default config object

"""

__copyright__ = 'this file is in the public domain'

# IMPORT SECTION

# gozerbot imports
from utils.exception import handle_exception
from persist.persist import Persist

# simplejson imports
from simplejson import loads, dumps

# basic imports
import types, logging, os

# END IMPORT

class Config(Persist):

    """ 
        config class is a dict containing json strings. is writable to file 
        and human editable.

        :param ddir: datadir where the config file lives
        :type ddir: string
        :param filename: filename of the config file
        :type filename: string
        :param inittxt: initial txt to fill the config file with
        :type inittxt: string
      
    """

    def __init__(self, ddir=None, filename=None, inittxt=None, *args, **kw):
        self.dir = ddir or 'gozerdata'
        self.filename = filename or 'config'
        self.cfile = self.dir + os.sep + self.filename
        Persist.__init__(self, self.cfile)
        if not self.data:
            self.data = {}
        self.configlist = []
        self.load()

    def has_key(self, key):
        return self.data.has_key(key)

    def __dict__(self):
        return dict(self.data)

    def __getitem__(self, item):
        if not self.data.has_key(item):
            return None
        else:
            return self.data.__getitem__(item)

    def __setitem__(self, item, value):
        self.data[item] = value

    def set(self, item, value):
        self.data.__setitem__(item, value)
        self.save()

    def reload(self):
        pass

    def load(self):

        """
            load the config file.

            .. literalinclude:: ../../gozerbot/config.py
                :pyobject: Config.load

        """

        if self.filename == 'mainconfig':
            import gozerbot
            self.data['version'] = "GOZERAPP 0.1 (%s) DEVELOPMENT" % gozerbot.__version__
            if self.data.has_key('loadlist') and not self.data['loadlist']:
                self.data['loadlist'] = ["alias", "all", "at", "chanperm", "choice", "code", "core", "count", "fleet", "googletalk", "grep", "ignore", "irc", "jabber", "job", "misc", "nickcapture", "nickserv", "not", "quote", "reload", "rest", "reverse", "size", "sort", "tail", "tell", "to", "underauth", "uniq", "user", "userstate", "stats"]
            self.data.setdefault('owner', [])
            self.data.setdefault('loglevel', 10)
            self.data.setdefault('loglist',  [])
            self.data.setdefault('quitmsg', "http://gozerbot.org")
            self.data.setdefault('mask', "700")
            self.data.setdefault('debug', 0)
            self.data.setdefault('nodb', 1)
            self.data.setdefault('jsonuser', "users.json")
            self.data.setdefault('loadlist', ["alias", "all", "at", "chanperm", "choice", "code", "core", "count", "fleet", "googletalk", "grep", "ignore", "irc", "jabber", "job", "misc", "nickcapture", "nickserv", "not", "quote", "reload", "rest", "reverse", "size", "sort", "tail", "tell", "to", "underauth", "uniq", "user", "userstate", "plug", "test"])
            self.data.setdefault('dotchars',  " .. ")
            self.data.setdefault('floodallow', 1)
            self.data.setdefault('auto_register', 0)
        elif 'fleet' in self.dir:
            self.data.setdefault("enable", 1)
            self.data.setdefault("type", "irc")
            self.data.setdefault("owner", [])
            self.data.setdefault("user", "botter@gozerbot.org")
            self.data.setdefault("nick", "gozerbot")
            self.data.setdefault("server", "localhost")
            self.data.setdefault("host", "gozerbot.org")
            self.data.setdefault("password", "")
            self.data.setdefault("port", 0)
            self.data.setdefault("ssl", 0)
            self.data.setdefault("ipv6", 0)
            self.data.setdefault("username", "gozerbot")
            self.data.setdefault("realname", "GOZERBOT")
            self.data.setdefault("defaultcc", "!")
            self.data.setdefault("quitmsg", "http://gozerbot.org")
            self.data.setdefault("bindhost", "")
            self.data.setdefault("nolimiter", 0)
            self.data.setdefault("nickservpass", "")
            self.data.setdefault("nickservtxt", ["set unfiltered on", ])
            self.data.setdefault("auto_register", 0)
            self.data.setdefault("stripident", 0)
            self.data.setdefault("loadlist", [])

mainconfigtxt = """# gozerbot config
#
#
# TAKE NOTE THAT FORMAT IS IN JSON NOW SO USE " not '

# GLOBAL OWNER

# example: owner = ["~bart@127.0.0.1"] (include ident if needed)
owner = []

# logging level
loglevel = 10

# list of plugins to log
loglist = []

# quit message
quitmsg = "http://gozerbot.org"

# botterdata dir mask
mask = "700"

# enable debugging
debug = 0

# database stuff

nodb = 0
dbtype = "sqlite"
dbname = "db/main.db"
dbhost = "localhost"  
dbuser = "bart"
dbpasswd = "mekker2"

# json backstore

#jsonuser = "users.json"

# loadlist
loadlist = ["alias", "all", "at", "chanperm", "choice", "code", "core", "count", "fleet", "googletalk", "grep", "ignore", "irc", "jabber", "job", "misc", "nickcapture", "nickserv", "not", "quote", "reload", "rest", "reverse", "size", "sort", "tail", "tell", "to", "underauth", "uniq", "user", "userstate", "plug", "test"]

# chars used to seperate result
dotchars = " .. "

# nr of lines before flood protect kicks in

floodallow = 2

# auto register new users 

auto_register = 0

"""


""" init txt for fleet config """


fleetbotconfigtxt = """# gozerbot fleet bot config
#
#
# TAKE NOTE THAT FORMAT IS IN JSON NOW SO USE " not '

## MAIN STUFF

# set to 0 to disable
enable = 1

# set type to jabber or irc
type = "irc"

# owner (irc/jabber)
owner = []

## CONNECTION STUFF

# user (jabber) .. needs to be full JID because server is taken from it
user = "botter@gozerbot.org"

# nick (irc)
nick = "gozerbot"

# server (irc)
server = "localhost"

# host (jabber) .. server to connect to
host = "gozerbot.org"

# password (irc and jabber)
password = ""

# port (irc and jabber) .. 0 uses default value
port = 0

# ssl (irc)
ssl = 0

# use ipv6 (irc)
ipv6 = 0

# bots username (irc)
username = "gozerbot"

# bots realname (irc)
realname = "GOZERBOT"

## OTHER STUFF

# default control character
defaultcc = "!"

# quit message
quitmsg = "http://gozerbot.org"

# bindhost .. uncomment to enable
bindhost = ""

# disable limiter (irc)
nolimiter = 0

# nickserv .. set pass to enable nickserv ident (irc)
nickservpass = ""
nickservtxt = ["set unfiltered on"]

# allow every command except OPER commands
auto_register = 0

# strip ident string
stripident = 0

loadlist = []

"""

# INIT SECTION

# main config object
config = Config(filename='mainconfig')

# END INIT
